#ifndef TFRMSTRUCT_H
#define TFRMSTRUCT_H

#include <QWidget>
#include <QTextBrowser>
#include <QTreeWidget>
#include <QSyntaxHighlighter>
#include <QHostAddress>
#include "tlrstructmodel.h"

namespace Ui {
class TFrmStruct;
}

class TFrmStruct : public QWidget
{
    Q_OBJECT

public:
    explicit TFrmStruct(QWidget *parent = nullptr);
    ~TFrmStruct();

    //重置
    void reset(int nindex);
    //读取数据
    void rdPara(const QString &nequidstr,int nparastaadd,const QHostAddress &nip,int nport);
    void rdParaSerial(const QString &nequidstr,int nparastaadd,int nserialaddr);
    //下发数据
    void wrPara(const QString &nequidstr,int nparastaadd,const QHostAddress &nip,int nport);
    //控制数据
    void ctlPara(const QString &nequidstr,int nparastaadd,const QHostAddress &nip,int nport);

private:
    Ui::TFrmStruct *ui;
    enum TUIType {
        uiFrame,    //刷新界面
    };

    //结构体数据分析器
    QTreeWidget *m_treeStructMonitor = new QTreeWidget(this);
    //结构体标准格式显示
    QTextEdit *m_teStructFormat = nullptr;
    //待分析结构体序号
    int m_index = -1;

    //UI处理
    void dataToUI(const TUIType &nuitype);
    //结构体解析到树
    void decodeStructToTree(int npreoffset,const TLRStruct &nlrstruct,const QByteArray nactmemdata,QTreeWidgetItem *prootitem = nullptr);
    //树恢复到结构体
    void encodeTreeToStruct(TLRStruct &nlrstruct);
    //遍历树条目到结构体
    void queryTreeItemToStruct(const QTreeWidgetItem *ptreeitem,TLRStruct &nlrstruct);
    //遍历结构体到树条目
    void queryStructToTreeItem(QTreeWidgetItem *ptreeitem,const TLRStruct &nlrstruct);

private slots:
    //结构体数据更新
    void slotStructDataUpdate();
    //分析树内容变化
    void slotTreeWidgetItemChanged(QTreeWidgetItem *item, int column);

};

#endif // TFRMSTRUCT_H
